﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.Data
{
    public sealed class FilterItemCollection<TField> : System.Collections.ObjectModel.Collection<FilterItem>
    {
        #region Public Members

        #region Copy

        public FilterItemCollection<TField> Copy()
        {
            FilterItemCollection<TField> items;

            items = new FilterItemCollection<TField>();
            foreach (var item in this)
                items.Add(item.Copy());

            return items;
        }

        #endregion

        #region AddRange

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public void AddRange(IEnumerable<FilterItem> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
            {
                if (item != null)
                    Add(item);
            }
        }

        #endregion
        #region FindFilterItem

        public FilterItem FindFilterItem(TField fieldId)
        {
            return FindFilterItem(fieldId.ToString());
        }
        public FilterItem FindFilterItem(string fieldName)
        {
            return (from o in this
                    where o.FieldName.Equals(fieldName, System.StringComparison.OrdinalIgnoreCase)
                    select o).FirstOrDefault();
        }

        #endregion
        #region GetFilterItemValue

        public string GetFilterItemValue(TField fieldId, string defaultValue)
        {
            return GetFilterItemValue(fieldId.ToString(), defaultValue);
        }
        public string GetFilterItemValue(string fieldName, string defaultValue)
        {
            var item = FindFilterItem(fieldName);
            if (item == null)
                return defaultValue;
            return item.Value;
        }

        #endregion
        #region Remove

        public void Remove(TField fieldId)
        {
            Remove(fieldId.ToString());
        }
        public void Remove(string fieldName)
        {
            FilterItem item;
            int iMax;

            if (string.IsNullOrWhiteSpace(fieldName))
                return;

            iMax = Count;
            for (int i = iMax - 1; i >= 0; i--)
            {
                item = this[i];
                if (item.FieldName.Equals(fieldName, System.StringComparison.OrdinalIgnoreCase))
                {
                    RemoveAt(i);
                    return;
                }
            }
        }

        #endregion
        #region RemoveAllExcept

        public void RemoveAllExcept(IEnumerable<TField> fieldIds)
        {
            var list = (from o in fieldIds
                        select o.ToString());
            RemoveAllExcept(list);
        }
        public void RemoveAllExcept(IEnumerable<string> fieldNames)
        {
            HashSet<string> index;
            FilterItem item;
            int iMax;

            if ((fieldNames != null) && (fieldNames.Any()))
            {
                index = new HashSet<string>(fieldNames, System.StringComparer.OrdinalIgnoreCase);
                iMax = this.Count;
                for (int i = iMax - 1; i >= 0; i--)
                {
                    item = this[i];
                    if (!index.Contains(item.FieldName))
                        RemoveAt(i);
                }
            }
            else
            {
                Clear();
                return;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public FilterItemCollection()
        {
        }

        #endregion
    }
}
